package com.selfdriving.activity;

import java.util.ArrayList;
import java.util.List;
import com.selfdriving.R;
import com.selfdriving.view.CircularImage;
import com.selfdriving.view.HorizontalListView;
import net.tsz.afinal.annotation.view.ViewInject;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class StrategyCommentActivity extends BaseActivity{
	@ViewInject(id = R.id.horList)
	HorizontalListView listView;

	@ViewInject(id = R.id.comment_list)
	ListView comment_list;
	
	@ViewInject(id = R.id.topBack, click = "topBack")
	ImageView topBack;

	@ViewInject(id = R.id.topText)
	TextView topText;

	@ViewInject(id = R.id.topRight, click = "topRight")
	Button topRight;

	private List<Person> list;

	private List<Person> comments;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.strategy_comment);
		list = new ArrayList<Person>();
		topText.setText("景点攻略");
		topRight.setVisibility(View.GONE);
		comments = new ArrayList<Person>();
		for (int i = 0; i < 12; i++) {
			Person person = new Person();
			list.add(person);
		}
		for (int i = 0; i < 10; i++) {
			Person person = new Person();
			comments.add(person);
		}
		HorAdapter adapter = new HorAdapter();
		listView.setAdapter(adapter);
		comment_list.setAdapter(new CommentAdapter());
		setListViewHeightBasedOnChildren(comment_list);
	}

	public class HorAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			convertView = getLayoutInflater().inflate(R.layout.person_icon_item, null);
			CircularImage icon = (CircularImage) convertView
					.findViewById(R.id.ivIcon);
			icon.setImageResource(R.drawable.person_icon_2);
			return convertView;
		}

	}

	private class CommentAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return comments.size();
		}

		@Override
		public Object getItem(int position) {
			return comments.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = getLayoutInflater().inflate(R.layout.strategy_comment_item,
					null);
			CircularImage icon = (CircularImage) convertView
					.findViewById(R.id.ivIcon);
			icon.setImageResource(R.drawable.person_icon_2);
			return convertView;
		}

	}

	private class Person {

	}
	
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

}

